package com.huawei.esdk.uc.sametime.popup.actions;

import org.apache.log4j.Logger;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.widgets.Display;

import com.huawei.esdk.uc.jna.Constants;
import com.huawei.esdk.uc.sametime.ConstantsDefines;
import com.huawei.esdk.uc.sametime.ConstantsDefines.CallStatus;
import com.huawei.esdk.uc.sametime.international.Messages;
import com.huawei.esdk.uc.sametime.models.Participant;
import com.huawei.esdk.uc.sametime.models.ParticipantList;
import com.huawei.esdk.uc.sametime.services.UCCallService;
import com.huawei.esdk.uc.sametime.util.GlobalConfiguration;
import com.huawei.esdk.uc.sametime.util.PluginUtil;
import com.huawei.esdk.uc.sametime.widgets.EsdkUCConvWindow;
import com.ibm.collaboration.realtime.people.Group;
import com.ibm.collaboration.realtime.people.PeopleUtil;
import com.ibm.collaboration.realtime.people.Person;

/**
 * 选中一个分组的时候,弹出的右键菜单上的电话会议控制类
 * @author y00175154
 */

public class EsdkConfGroupActionDelegate extends AbstractObjectActionDelegate
{
    private static Logger logger = Logger
            .getLogger(EsdkConfGroupActionDelegate.class);

    public void run(IAction action)
    {
        Group[] groups = getSelectedGroups();
        ParticipantList participantList = new ParticipantList();
        for (int i = 0; i < groups.length; i++)
        {
            Person[] persons = groups[i].getPersons();
            if (null != persons)
            {
                for (int j = 0; j < persons.length; j++)
                {
                    if (PeopleUtil.isMe(persons[j]))
                    {
                        continue;
                    }
                    Participant participant = new Participant();
                    participant.setContactId(persons[j].getContactId());
                    String ucAccount = PluginUtil
                            .getUCAccountFromContactID(persons[j].getContactId());
                    participant.setNumber(ucAccount);
                    participant
                            .setCallNumType(Constants.AccountType.UC_ACCOUNT);
                    participantList.addparticipants(participant);
                }
            }
        }
        // 如果当前没有通话,则可以发起电话会议,否则,给出提示
        if (ConstantsDefines.CallStatus.end == UCCallService.getCallStatus())
        {
            try
            {
                EsdkUCConvWindow window = EsdkUCConvWindow.getInstance();
                if(participantList.getparticipants().size()>=2)
                {
                    window.setConfEnabled(true);
                    Person person = PeopleUtil.getLocalPerson();
                    Participant participant = new Participant();
                    participant.setContactId(person.getContactId());
                    participant.setNumber(PluginUtil.getUCAccountFromContactID(person.getContactId()));
                    participant.setCallNumType(Constants.AccountType.UC_ACCOUNT);
                    participantList.getparticipants().add(0, participant);
                }
                window.createUCConvWindow(participantList);
                window.makeCall(false);
            }
            catch (Exception exception)
            {
                logger.error(exception.getMessage());
            }
        }
        else
        {
            MessageDialog.openInformation(
                    Display.getCurrent().getActiveShell(),
                    Messages.TIPS,
                    Messages.IS_CALLING);
        }

    }

    @Override
    public void selectionChanged(IAction action, ISelection selection)
    {
        super.selectionChanged(action, selection);

        // UC没有登录或者sametime没有登录的时候,上下文菜单中的电话会议菜单项不应该被激活
        boolean isUCLogin = GlobalConfiguration.getInstance().isUCLogin();
        boolean isSametimeLogin = GlobalConfiguration.getInstance().isSamtimeLogin();
        boolean isInCall = (CallStatus.end != UCCallService.getCallStatus());
        
        if (isUCLogin && isSametimeLogin && !isInCall)
        {
            action.setEnabled(true);
        }
        else
        {
            action.setEnabled(false);
        }
    }

}
